package test;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;


public class StringFormatJson {

	public static void main(String[] args) {
		String origin = "1. 康复科（康复医学科专科3039、腰背痛康复3065、脊柱侧弯康复3066、认知和学习能力3068、运动损伤康复3069、产后康复3067、吞咽障碍康复3097）\r\n"

				+ "123456";

		// 取到每个 1. 序号后面的内容
		String newOrigin = origin.substring(origin.indexOf('\n') + 1);

		int dept2CodeFront = 2301; // 二级科室前缀码
		
		List<Dept2> dept2List = new ArrayList<>();
		while (!origin.equals("123456")) {
			String subOrigin1 = origin.substring(origin.indexOf(".") + 1, origin.indexOf('\n')).trim();
			String newOrigin1 = origin.substring(origin.indexOf('\n') + 1);
			System.out.println("========================");
			System.out.println(subOrigin1);
			
			dept2List.add(getDept2(subOrigin1,  Integer.toString(dept2CodeFront)));
			dept2CodeFront ++;
			origin = newOrigin1;
		}
		
		Dept1 dept1 = new Dept1();
		dept1.setYjksid("23");
		dept1.setYjksmc("康复");
		dept1.setDept2List(dept2List);
		
		System.out.println(JSONObject.toJSONString(dept1));
		
	}

	/**
	 * 从传入的 e.g.【1. 心血管内科（专家、心血管内科专科3112、高血压门诊3042）】中获取dept2
	 * 
	 * @author: 1952593542
	 * @Date: 2022年11月3日上午12:08:10
	 * @param origin
	 * @return
	 */
	public static Dept2 getDept2(String subOrigin, String secendDeptCode) {
		Dept2 dept2 = new Dept2();

		// 取到三级科室的所有内容
		String s3 = subOrigin.substring(subOrigin.indexOf('（') + 1, subOrigin.lastIndexOf('）'));
//		System.out.println("secentDeptDetail=" + s3);
		// 取二级科室名
		String secentDeptName = subOrigin.substring(0, subOrigin.indexOf('（'));
//		System.out.println("secentDeptName=" + secentDeptName);

		String[] dept3Arr = s3.split("、");
		List<Dept3> temps = new ArrayList<>();

		for (int i = 0; i < dept3Arr.length; i++) {
			char[] c1 = dept3Arr[i].toCharArray();
			boolean existNumber = false;

			for (int j = 1; j < c1.length; j++) {
				if (c1[j] < '9' && c1[j] > '0') {
					String deptName = dept3Arr[i].substring(0, j);
					String deptCode = dept3Arr[i].substring(j);
					Dept3 dept3 = new Dept3();
					dept3.setKsdm(deptCode);
					dept3.setKsmc(deptName);
					temps.add(dept3);
					existNumber = true;
					break;
				}
			}

			if (!existNumber) {
				Dept3 dept3 = new Dept3();
				dept3.setKsdm("");
				dept3.setKsmc (dept3Arr[i]);
				temps.add(dept3);
			}
		}

		dept2.setEjksmc(secentDeptName);
		dept2.setDept3List(temps);
//		dept2.setEjksdm("");
		dept2.setEjksdm(secendDeptCode);
		return dept2;
	}

}
